package com.longge;

public class AlternatingSubarray {

    public int alternatingSubarray(int[] nums) {

        int preIndex = 0, res = -1;
        for (int i = 1; i < nums.length; i++) {
            int len = i - preIndex + 1;
            if (nums[i] - nums[preIndex] == (len - 1) % 2) {
                res = Math.max(res, len);

            } else {
                if (nums[i] - nums[i - 1] == 1) {
                    preIndex = i - 1;
                    res = Math.max(res, 2);
                } else {
                    preIndex = i;
                }
            }
        }
        return res;

    }
}
